﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Applee.FractCommon;
using System.Windows.Forms;
using System.Drawing;

namespace Applee.SierpinskiFract
{
    public class RectangleFract : BaseFract
    {
        protected override void Initialize()
        {
            SizeF shapeSize = new SizeF(1.0F, 1.0F); //box边比1:1
            float flength = 0;
            float fMargin = 10;

            if (MathUtils.CalcAdaptiveLength(mViewSize, shapeSize, ref flength))
            {
                //width
                mLength = 1.0F * flength - fMargin * 2.0F;
            }
            else
            {
                //Height
                mLength = 1.0F * flength - fMargin * 2.0F;
            }
        }

        protected override void CreateFract(PointF point, RandomColor color, float fLength, int iterations)
        {
            if (iterations < 0)
                return;

            if (fLength < 1.0F)
                return;
            
            Color[] colors = color.GetColor2(0, 2);

            SierRectangle et1 = new SierRectangle();
            et1.MakeShape(point, fLength);
            et1.Color = colors[0];

            mFractObjects.Add(et1);

            SierRectangle et2 = new SierRectangle();
            et2.MakeShape(point, fLength / 3.0F);
            et2.Color = colors[1];

            mFractObjects.Add(et2);

            SierRectangle etTemp = new SierRectangle();
            etTemp.MakeShape(point, fLength / 3.0F * 2);

            CreateFract(etTemp.Points[0], color, fLength / 6.0F * 2, iterations - 1);
            CreateFract(etTemp.Points[1], color, fLength / 6.0F * 2, iterations - 1);
            CreateFract(etTemp.Points[2], color, fLength / 6.0F * 2, iterations - 1);
            CreateFract(etTemp.Points[3], color, fLength / 6.0F * 2, iterations - 1);
            CreateFract(MathUtils.Midpoint(etTemp.Points[0], etTemp.Points[1]), color, fLength / 6.0F * 2, iterations - 1);
            CreateFract(MathUtils.Midpoint(etTemp.Points[1], etTemp.Points[2]), color, fLength / 6.0F * 2, iterations - 1);
            CreateFract(MathUtils.Midpoint(etTemp.Points[2], etTemp.Points[3]), color, fLength / 6.0F * 2, iterations - 1);
            CreateFract(MathUtils.Midpoint(etTemp.Points[3], etTemp.Points[0]), color, fLength / 6.0F * 2, iterations - 1);
        }
    }
}
